package net.aiion.weave.impl.cards.expressions;

import net.aiion.weave.spi.cards.CardProperties;
import net.aiion.weave.spi.cards.expressions.CardExpression;
import net.aiion.weave.spi.cards.expressions.CardExpressionResolver;
import net.aiion.weave.spi.injection.annotations.Inject;
import net.aiion.weave.spi.naming.annotations.Named;
import net.aiion.weave.spi.naming.annotations.UserScoped;

// TODO Properties verallgemeinern und PropertyService in neuem Modul entwickeln. Dort dann diesen Resolver hin, Scope für den Resolver: DynamicScoped.
@UserScoped
@Named
public class CardPropertyExpressionResolver implements CardExpressionResolver
{
    private static final String PROPERTY_PREFIX = "property";

    @Inject
    private CardProperties cardProperties;

    public CardPropertyExpressionResolver() {
    }

    @Override
    public boolean resolvesExpression(final CardExpression aCardExpression) {
        return PROPERTY_PREFIX.equals(aCardExpression.getPrefix()) || "".equals(aCardExpression.getPrefix());
    }

    @Override
    public Object readExpression(final CardExpression aCardExpression) {
        return cardProperties.getProperty(aCardExpression.getValue());
    }

    @Override
    public void writeExpression(final CardExpression aCardExpression, final Object aValue) {
        cardProperties.setProperty(aCardExpression.getValue(), aValue);
    }
}
